package stack;

public class MyStack {
    private int index;
    private int maxSize;
    private int[] stack;

    public MyStack(int maxSize){
        this.maxSize = maxSize;
        stack = new int[maxSize];
        index = -1;
    }

    public void push(int value){
        //判断满
        if (isFull()){
            throw new RuntimeException("栈已满");
        }
        index++;
        stack[index] = value;
    }

    //
    public int pop(){
        //判空
        if (isEmp()){
            throw new RuntimeException("栈kong");
        }
        int temp = stack[index];
        index--;
        return temp;
    }

    //返回栈顶元素
    public int peek(){
        if (isEmp()){
            throw new RuntimeException("栈空");
        }
        return stack[index];
    }

    //判空
    private boolean isEmp() {
        return index ==-1;
    }
    //返回最小值
    public int min(){
        return 0;
    }

    //判断是否为满
    public boolean isFull(){
        return index+1 == maxSize;
    }
    //遍历
    public void ergodic(){
        if (isEmp()){
            throw new RuntimeException("栈空");
        }
        for (int i = 0; i < index; i++) {
            System.out.println("栈第"+i+"个数为"+stack[i]);
        }
    }
}
